﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace PadiCalClient
{
    public class pending
    {
        private int id;

        private string name;

        private string state;

        Reservation reservation;

        private ArrayList Ackedslots;

        //private string currentSlot;

        private int numUsers;

        private ArrayList AckedUsers = new ArrayList();

        public pending(Reservation res)
        {
            id = res.getID();
            this.name = res.Name;
            this.state = res.State;
            this.reservation = res;
            this.numUsers = res.invUsers.Count-1;
            //this.AckedUsers.Add(res.Host);
            this.Ackedslots = res.Slots;


        }

        public int getID()
        {
            return id;
        }

        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }

        public string State
        {
            get { return this.state; }
            set { this.state = value; }
        }

        public int countUsers
        {
            get { return this.numUsers; }
            set { this.numUsers = value; }
        }

        public ArrayList UsersAcked
        {
            get { return this.AckedUsers; }
            set { this.AckedUsers = value; }
        }

        public Reservation Reservation
        {
            get { return this.reservation; }
        }


        public bool acked(string Ruser, ArrayList Rslots)
        {
            if (!this.AckedUsers.Contains(Ruser))
            {
                this.AckedUsers.Add(Ruser);

                if (State.Equals("Initiated"))
                {
                    //this.AckedUsers.Add(Ruser);

                    ArrayList resul = new ArrayList();

                    foreach (string sl in this.Ackedslots)
                    {
                        if (Rslots.Contains(sl))
                            resul.Add(sl);
                    }

                    this.Ackedslots = resul;

                    if (this.Ackedslots.Count == 0)
                        return false;
                }
                /*else if (State.Equals("Tentative"))
                {
                    // VErificar se é necessário algo
                }*/
            }

            return true;
        }

        public Reservation removeTentatively()
        {
            if (reservation.changeSlot())
            {
                AckedUsers.Clear();
                state = reservation.State;
                return reservation;
            }
            else
                return null;
        }

        public Reservation changeToTentatively()
        {
            Reservation ret = null;
            if (State.Equals("Initiated"))
            {
                this.reservation.changeToTentatively(this.Ackedslots);
                this.state = this.reservation.State;
                this.AckedUsers.Clear();
                ret =  this.reservation;
            }
            return ret;
        }

        public Reservation changeToCommit()
        {
            Reservation ret = null;
            if (State.Equals("Tentatively"))
            {
                this.reservation.changeToCommit();
                this.state = this.reservation.State;
                this.AckedUsers.Clear();
                ret = this.reservation;
            }
            return ret;
        }

        public Reservation changeToCommited()
        {
            Reservation ret = null;
            if (State.Equals("Commit"))
            {
                this.reservation.changeToCommited();
                this.state = this.reservation.State;
                this.AckedUsers.Clear();
                ret = this.reservation;
            }
            return ret;
        }

    }
}
